package com.franklin.ideaplugin.easytesting.ui;

import com.franklin.ideaplugin.api.ui.editor.CustomEditor;
import com.franklin.ideaplugin.easytesting.rpc.entity.MethodParameter;
import com.intellij.psi.PsiParameter;
import lombok.RequiredArgsConstructor;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Map;

/**
 * @author Ye Junhui
 * @since 2023/5/22
 */
@RequiredArgsConstructor
public class MyKeyListener implements KeyListener {

    private final JComponent textField;
    private final PsiParameter psiParameter;
    private final Map<String,MethodParameter> paramMap;

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {
        String text = "";
        if (textField instanceof JTextField){
            JTextField field = (JTextField) textField;
            text = field.getText();
        }else if (textField instanceof CustomEditor){
            CustomEditor customEditor = (CustomEditor) textField;
            text= customEditor.getText();
        }
        paramMap.put(
                psiParameter.getName(),
                new MethodParameter(psiParameter.getType().getCanonicalText(),text)
        );
    }
}
